package myclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @author JIAO
 */
class ReaderThread extends Thread{
	BufferedReader reader ;
	HashMap<String,TalkFrame> talkMap; 
	HashMap<String,FriendMessage> list;
	MainFrame mainFm;
	DefaultMutableTreeNode node;
	Vector<String> v = new Vector<String>();
	SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
	ReaderThread(BufferedReader reader,HashMap<String,TalkFrame> talkMap,
			    HashMap<String,FriendMessage> list,MainFrame frame,
			    DefaultMutableTreeNode node){
		this.reader = reader;
		this.talkMap = talkMap;
		this.list = list;
		this.mainFm = frame;
		this.node = node;
	}
	public void run(){
		try {
			while(true){
				String sourceID = reader.readLine();
				StringBuffer strAll = new StringBuffer();
				String line;
				String fu = "";
				if(sourceID.equals("server")){
//					JOptionPane.showMessageDialog(null, "server");
					String thingName = reader.readLine();
//					JOptionPane.showMessageDialog(null, thingName);
					if(thingName.equals("searchFriend")){
						while(true){
							String id = reader.readLine();
							if(id.equals("-1")){
								JOptionPane.showMessageDialog(null, "没有此好友");
								break;
							}else if(id.equals("end"))
								break;
							else
								v.add(id+"   "+reader.readLine());
						}
						mainFm.lb_listener.search.searchReturn();
					}else if(thingName.equals("addFriend")){//关于添加好友的情况
						System.out.println("server发来添加好友的信息");
						if(reader.readLine().equals("1")){
							JOptionPane.showMessageDialog(null, "好友添加成功！");
							String id = reader.readLine();
							String name = reader.readLine();
							String pic = reader.readLine();
							String status = reader.readLine();
							String motto = reader.readLine();
							String birthday = reader.readLine();
							String city = reader.readLine();
							System.out.println(id+"-->"+name+"-->"+pic+"-->"+status);
							list.put(id, new FriendMessage(name,pic,status,motto,birthday,city));
							node.add(new DefaultMutableTreeNode(id));
							mainFm.model.reload();
						}
						else
							JOptionPane.showMessageDialog(null,"号码不存在！,请检查"); 
						continue;
					}else if(thingName.equals("deleteFriend")){
						if(reader.readLine().equals("1")){
							JOptionPane.showMessageDialog(null, "好友删除成功！");
							String id = reader.readLine();
							System.out.println(id);
							list.remove(id);
							System.out.println("size:"+list.size());
							mainFm.flush();
						}else
							JOptionPane.showMessageDialog(null,"号码不存在！,请检查"); 
					}else if(thingName.equals("status")){//关于好友状态的改变的信息
//						System.out.println("server端发来用户信息改变的信息");
//						FileInputStream fileau=new FileInputStream("Classic\\Global.wav");   
//	                    AudioStream as=new AudioStream(fileau);   
//	                    AudioPlayer.player.start(as); 
						line = reader.readLine();
						list.get(line).changeStatus();
						mainFm.model.reload();
					}else if(thingName.equals("sendFile")){
						String friendID = reader.readLine();
						String fileName = reader.readLine();
						System.out.println("有人发来发送文件信息");
						if(mainFm.talkMap.containsKey(friendID)){
							mainFm.talkMap.get(friendID).addPn(fileName);
						}else{
							TalkFrame talk = new TalkFrame(friendID, mainFm);
							
							talk.addPn(fileName);
							mainFm.talkMap.put(friendID, talk);
						}
					}else if(thingName.equals("toFile")){
						String or = reader.readLine();
						String friendID = reader.readLine();
						String fileName = reader.readLine();
						if(or.equals("0")){
							mainFm.talkMap.get(friendID).area1.append("好友拒绝了接收文件："+fileName+"\n");
						}else if(or.equals("1")){
							int port = Integer.parseInt(reader.readLine());
							int num = Integer.parseInt(reader.readLine());
							File f = mainFm.talkMap.get(friendID).fileMap.get(fileName);
							Thread t = new ClientFileTransfer("localhost",port,1,f,num,friendID,mainFm);
							t.start();
						}
					}else if(thingName.equals("getFile")){
						String friendID = reader.readLine();//= 
						String fileName = reader.readLine();
						int port = Integer.parseInt(reader.readLine());
						int num = Integer.parseInt(reader.readLine());
						Thread t = new ClientFileTransfer(mainFm.ip,port,0,new File("F:\\temp\\"+fileName),num,friendID,mainFm);
						t.start();
					}
				}else{//用户好友发来的聊天信息
//					Player play = null;
//					try {
//						play = Manager.createRealizedPlayer(new File("/Classic\\msg.wav").toURI().toURL());
//					} catch (NoPlayerException e) {
//						e.printStackTrace();
//					} catch (CannotRealizeException e) {
//						e.printStackTrace();
//					}
//					play.prefetch();
//					play.start();
					while((line=reader.readLine())!=null){
						if(line.equals("end")){//当返回的字符串为end时结束
							fu = "";
							break;
							}
						strAll.append(fu+line);
						fu = "\n";
					}
					strAll.append("\n");
					if(talkMap.containsKey(sourceID)){
						TalkFrame talkFrame = talkMap.get(sourceID);
						talkFrame.area1.append(strAll.toString());
					}else{
						if(mainFm.haveMsgMap.containsKey(sourceID)){
							mainFm.haveMsgMap.put(sourceID, 
									mainFm.haveMsgMap.get(sourceID)+strAll.toString());
						}else{
							mainFm.haveMsgMap.put(sourceID, strAll.toString());							
						}
						mainFm.t.start();
					}
				}
				
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
